#!/usr/bin/python -OO
# -*- coding: utf-8 -*-

# Copyright © 2008,2009 Trust Digital Technology Laboratory, Shanghai Jiao Tong University.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Author:
#   XING Dong sheng <homer.xing@gmail.com>

#需要 sudo apt-get install python-xlib -y

try:
	import Xlib
except:
	import os
	print "Run 'sudo apt-get install python-xlib -y'"
	os.system('sudo apt-get install python-xlib -y')
	try:
		import Xlib
	except:
		sys.exit(0)
import Xlib.display
import Xlib.ext.xtest

class ControlMouse:
	def __init__(self):
		self.display = Xlib.display.Display()
		self.screen = self.display.screen()
		self.root = self.screen.root

	def mouse_click(self, button):
		self.mouse_down(button)
		self.mouse_up(button)

	def mouse_down(self, button): #button=1 left, 2 middle, 3 right
		Xlib.ext.xtest.fake_input(self.display,Xlib.X.ButtonPress, button)
		self.display.sync()

	def mouse_up(self, button): #button=1 left, 2 middle, 3 right
		Xlib.ext.xtest.fake_input(self.display,Xlib.X.ButtonRelease, button)
		self.display.sync()

	def mouse_move(self, x,y): #移动鼠标
		self.root.warp_pointer(x,y)
		self.display.sync()

	def get_screen_resolution(self):
		return self.screen['width_in_pixels'], self.screen['height_in_pixels']

# example
# T=ControlMouse()
# print T.get_screen_resolution()
# T.mouse_move(20,50)
# T.mouse_click(1)

